const bgSettingModel = appRequire('models/bgSetting');
const tokenHelper = appRequire('common/tokenHelper');
const { Op } = require("sequelize");
var qiniu = require("qiniu");
// 导出对象，供其它模块调用
module.exports = {
    createrCanIn,
    iosPay,
    getToken
};

async function getToken(req, response) {
    let postData = req.body
    const returnData = Common.clone(Constant.DEFAULT_SUCCESS);


    //需要填写你的 Access Key 和 Secret Key
    let accessKey = '3hOARVprmGFiKcb1MgcwJxSgZkUOe_zUyjPXUlet';
    let secretKey = 'wzxXaNfuhhDdJiYaEIwEQvKYe3_7QmJnvXzTpKkg';
    //要上传的空间
    bucket = 'artppt109063';
    //上传到七牛后保存的文件名
    key = postData.fileName;
    var options = {
        scope: bucket
    }
    var mac = new qiniu.auth.digest.Mac(accessKey, secretKey);
    var options = {
        scope: bucket
    }
    var putPolicy = new qiniu.rs.PutPolicy(options);

    returnData.data = putPolicy.uploadToken(mac);
    response.json(returnData)
}

async function createrCanIn(req, response) {
    let postData = req.body
    const returnData = Common.clone(Constant.DEFAULT_SUCCESS);

    /*let userIds = ["3gI2zAg9Zn8RJZ8", "3h4SfJjFn9J4SHA", "3h0dWm6kJSFTehY"]
    if (userIds.includes(postData.uid)) {
        returnData.data.canIn = true
    }*/

    if (postData.appSource == "H5") {
        returnData.data = false
    } else {
        let result = await bgSettingModel.findOne({
            where: {
                objectId: "11231231",
            }
        })
        result = Common.clone(result)
        returnData.data = false
    }

    response.json(returnData)
}

async function iosPay(req, response) {
    let postData = req.body
    const returnData = Common.clone(Constant.DEFAULT_SUCCESS);

    // 从系统配置读取 iOS 功能开关
    const iosCanIn = await tokenHelper.getIosCanIn();

    let thisResult = {
        canIn: true // 默认可用
    };

    // 如果是 iOS 用户，则使用系统配置的值
    if (postData.osName == "ios") {
        thisResult.canIn = iosCanIn === '1'; // '1' 转为 true, '0' 转为 false
    }

    returnData.data = thisResult;
    response.json(returnData);
}